package org.sword.wechat4j.driver;

import org.sword.wechat4j.type.EventType;
import org.sword.wechat4j.type.MsgType;
import org.sword.wechat4j.request.*;
import org.sword.wechat4j.response.WechatResponse;

/**
 * 事件驱动
 * Created by sean on 2016-07-22-0022.
 */
public abstract class EventDriver extends EventDriverI {

    @Override
    public final WechatResponse service(BaseMessage baseMessage) {
        WechatResponse wechatResponse = super.service(baseMessage);
        if (MsgType.event.name().equals(baseMessage.getMsgType()))
            distributeEvent((BaseEventMessage) baseMessage);
        return wechatResponse;
    }

    /**
     * event事件分发
     */
    private void distributeEvent(BaseEventMessage eventMessage) {
        EventType event = EventType.valueOf(eventMessage.getEvent());
        switch (event) {
            case CLICK:
                menuClick((MenuClickEventMessage) eventMessage);
                break;
            case subscribe:
                subscribe((SubscribeScanEventMessage) eventMessage);
                break;
            case unsubscribe:
                unSubscribe((UnsubscribeEventMessage) eventMessage);
                break;
            case SCAN:
                scan((SubscribeScanEventMessage) eventMessage);
                break;
            case LOCATION:
                location((LocationEventMessage) eventMessage);
                break;
            case VIEW:
                view((MenuViewEventMessage) eventMessage);
                break;
            case scancode_push:
                scancodePush((ScancodePushWaitmsgEventMessage) eventMessage);
                break;
            case scancode_waitmsg:
                scancodeWaitMsg((ScancodePushWaitmsgEventMessage) eventMessage);
                break;
            case pic_sysphoto:
                picSysPhoto((PicEventMessage) eventMessage);
                break;
            case pic_photo_or_album:
                picPhotoOrAlbum((PicEventMessage) eventMessage);
                break;
            case pic_weixin:
                picWeixin((PicEventMessage) eventMessage);
                break;
            case location_select:
                locationSelect((LocationSelectEventMessage) eventMessage);
                break;
            case MASSSENDJOBFINISH:
                massSendJobFinish((MassSendJobFinishEventMessage) eventMessage);
                break;
            case TEMPLATESENDJOBFINISH:
                templateSendJobFinish((TemplateSendJobFinishEventMessage) eventMessage);
                break;
            default:
                otherEvent(eventMessage);
                break;
        }
    }

    /**
     * subscribe关注事件处理
     * @param eventMessage 事件
     */
    protected abstract void subscribe(final SubscribeScanEventMessage eventMessage);

    /**
     * unSubscribe取消关注事件处理
     * @param eventMessage 事件
     */
    protected abstract void unSubscribe(final UnsubscribeEventMessage eventMessage);

    /**
     * scan事件处理
     * @param eventMessage 事件
     */
    protected abstract void scan(final SubscribeScanEventMessage eventMessage);

    /**
     * location事件处理event=location
     * @param eventMessage 事件
     */
    protected abstract void location(final LocationEventMessage eventMessage);

    /**
     * (菜单事件) click点击事件处理
     * @param eventMessage 事件
     */
    protected abstract void menuClick(final MenuClickEventMessage eventMessage);

    /**
     * (菜单事件) view 事件处理
     * @param eventMessage 事件
     */
    protected abstract void view(final MenuViewEventMessage eventMessage);

    /**
     * (菜单事件) 扫码推事件的事件推送
     * @param eventMessage 扫码推事件
     */
    protected abstract void scancodePush(final ScancodePushWaitmsgEventMessage eventMessage);

    /**
     * (菜单事件) 扫码推事件且弹出“消息接收中”提示框的事件推送
     * @param eventMessage 扫码推事件
     */
    protected abstract void scancodeWaitMsg(final ScancodePushWaitmsgEventMessage eventMessage);

    /**
     * (菜单事件) 弹出系统拍照发图的事件推送
     * @param eventMessage 系统拍照事件
     */
    protected abstract void picSysPhoto(final PicEventMessage eventMessage);

    /**
     * (菜单事件) 弹出拍照或者相册发图的事件推送
     * @param eventMessage 拍照或发图事件
     */
    protected abstract void picPhotoOrAlbum(final PicEventMessage eventMessage);

    /**
     * (菜单事件) 弹出微信相册发图器的事件推送
     * @param eventMessage 微信相册发图器事件
     */
    protected abstract void picWeixin(final PicEventMessage eventMessage);

    /**
     * (菜单事件) 弹出地理位置选择器的事件推送
     * @param eventMessage 弹出地理位置选择器事件
     */
    protected abstract void locationSelect(final LocationSelectEventMessage eventMessage);

    /**
     * 群发结果通知 事件处理
     * @param eventMessage 事件
     */
    protected abstract void massSendJobFinish(final MassSendJobFinishEventMessage eventMessage);

    /**
     * 模板消息推送结果通知 事件处理
     * @param eventMessage 事件
     */
    protected abstract void templateSendJobFinish(final TemplateSendJobFinishEventMessage eventMessage);

    /**
     * 其他未定义事件处理
     * @param eventMessage 事件消息
     */
    protected abstract void otherEvent(BaseEventMessage eventMessage);
}
